package httpHandler

import (
	"gitee.com/zaiqiang231/lovesport-user-service/app/http/service"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model/req"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
)

type SysAuthorityHandler struct {
}

func (*SysAuthorityHandler) GetAuthorityList(c *gin.Context) {
	var req req.PageInfo
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	list, err := service.ServiceAuthorityInstance.GetAuthorityList(req)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", list)
}

func (*SysAuthorityHandler) GetAllAuthorityList(c *gin.Context) {
	list, err := service.ServiceAuthorityInstance.GetAllAuthorityList()
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", list)
}

func (*SysAuthorityHandler) CreateAuthority(c *gin.Context) {
	var auth model.SysAuthority
	if err := c.ShouldBindBodyWith(&auth, binding.JSON); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	err := service.ServiceAuthorityInstance.CreateAuthority(&auth)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", auth)
}

func (*SysAuthorityHandler) DeleteAuthority(c *gin.Context) {
	var auth model.SysAuthority
	if err := c.ShouldBindBodyWith(&auth, binding.JSON); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	err := service.ServiceAuthorityInstance.DeleteAuthority(&auth)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", auth)
}

func (*SysAuthorityHandler) UpdateAuthority(c *gin.Context) {
	var auth model.SysAuthority
	if err := c.ShouldBindBodyWith(&auth, binding.JSON); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	err := service.ServiceAuthorityInstance.UpdateAuthority(&auth)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", auth)
}

func (*SysAuthorityHandler) CopyAuthority(c *gin.Context) {
	var req req.SysAuthorityCopyReq
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	err := service.ServiceAuthorityInstance.CopyAuthority(&req)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", nil)
}
